<?php


namespace App\Http\Controllers\Api\v1;


use App\common;
use App\Http\Controllers\Controller;
use App\Logic\GoodsLogic;
use App\Server\BaiduServer;
use Illuminate\Http\Request;


/**
 * kys
 * Class GoodsController
 * @package App\Http\Controllers\Api\v1
 *商品模块控制器
 */
class GoodsController extends Controller
{

    /**
     * 商品列表展示
     * @return array|false|string
     * kys
     */
    public function GoodsList()
    {
        try {
            $goods = GoodsLogic::GoodsList();
            return common::success(20000, $goods, '商品信息展示成功');
        } catch (\Exception $exception) {
            return common::fail(201, '', $exception->getMessage());
        }
    }

    /**
     * 商品添加
     * @param Request $request
     * @return array|false|string
     * kys
     */
    public function GoodsAdd(Request $request)
    {
        $goodsName = $request->post('goods_name');
        $post = $request->post();
        try {
//        调用server 审核文字
            $res = (new BaiduServer())->contentAudit($goodsName);
            $poost = GoodsLogic::GoodsAdd($post);
            return common::success(20000, $poost, '商品添加成功');
        } catch (\Exception $exception) {
            return common::fail('201', "", $exception->getMessage());
        }
    }
    /**
     * 商品信息删除
     * @param Request $request
     * @return array|false|string
     * kys
     */
    public function GoodsDel()
    {
        try {
            $goodsId = request()->get('id');
            $goodsDel = GoodsLogic::GoodsDel($goodsId);
            return common::success(20000, $goodsDel, '商品信息删除成功');
        } catch (\Exception $exception) {
            return common::fail('201', '', $exception->getMessage());
        }
    }

    /**
     * 商品详情展示
     * @param Request $request
     * @return array|false|string
     * kys
     */
    public function GoodsDetail()
    {
        try {
            $detailId = request()->get('id');
            $detailMessage = GoodsLogic::GoodsDetails($detailId);
            return common::success(20000, $detailMessage, '商品详情展示成功');
        } catch (\Exception $exception) {
            return common::fail(201, '', $exception->getMessage());
        }
    }

    /**
     * 商品信息搜索
     * @return array|false|string
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     * kys
     */
    public function GoodsSeek()
    {
        $keyword = request()->get('keyword');

        try {

            $searchMessage = GoodsLogic::GoodsSeek($keyword);
            return common::success(20000, $searchMessage, '商品搜索成功');
        } catch (\Exception $exception) {
            return common::fail('201', '', $exception->getMessage());
        }
    }

    /**
     * 修改商品信息
     * @return array|false|string
     * kys
     */
    public function GoodsUpdate(Request $request)
    {
        try {
            $GoodsUpdate = $request->post()['data'];
            $GoodsAdd = GoodsLogic::GoodsUpdated($GoodsUpdate);
            return common::success(20000, $GoodsAdd, '商品信息修改成功');
        } catch (\Exception $exception) {
            return common::fail('201', '', $exception->getMessage());
        }
    }

    /**
     * 商品es搜索
     */
    public function GoodsSearch(Request $request){
        try {
            $keyword=$request->get('keyword');

            $SecondaryMessage=GoodsLogic::SecondarySearch($keyword);
            return common::success(20000,$SecondaryMessage,'商品es搜索成功');
        }catch (\Exception $exception){
            return common::fail('',"",$exception->getMessage());
        }
    }


}
